package com.example.everying.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.everying.mapper.user.PointsTransactionMapper;
import com.example.everying.model.entity.user.PointsTransaction;
import com.example.everying.model.entity.user.UserEntity;
import com.example.everying.service.user.PointsTransactionService;
import com.example.everying.service.user.UserService;
import com.example.everying.utils.CampusException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 积分流水Service实现类
 */
@Service
public class PointsTransactionServiceImpl extends ServiceImpl<PointsTransactionMapper, PointsTransaction> implements PointsTransactionService {

    @Autowired
    private UserService userService;

    /**
     * @param pageParam
     * @param query
     * @return
     */
    @Override
    public IPage<PointsTransaction> selectPage(Page<PointsTransaction> pageParam, PointsTransaction query) {
        if (query == null) {
            // 默认按创建时间倒序排列
            LambdaQueryWrapper<PointsTransaction> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByDesc(PointsTransaction::getCreateTime);
            return this.page(pageParam, queryWrapper);
        }
        LambdaQueryWrapper<PointsTransaction> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(query.getUserId() != null, PointsTransaction::getUserId, query.getUserId());
        queryWrapper.eq(query.getTransactionType() != null, PointsTransaction::getTransactionType, query.getTransactionType());
        queryWrapper.eq(query.getStatus() != null, PointsTransaction::getStatus, query.getStatus());
        queryWrapper.like(query.getDescription() != null, PointsTransaction::getDescription, query.getDescription());
        queryWrapper.like(query.getTransactionNo() != null, PointsTransaction::getTransactionNo, query.getTransactionNo());
        // 默认按创建时间倒序排列
        queryWrapper.orderByDesc(PointsTransaction::getCreateTime);
        return this.page(pageParam, queryWrapper);
    }

    @Override
    public boolean checkUserReceived(String userId,String status) {

        QueryWrapper<UserEntity> user = new QueryWrapper<>();
        user.eq("id", userId);
        user.eq("is_delete" , 0);

        UserEntity userEntity = userService.getOne(user);
        if (userEntity == null) {
            throw new CampusException("用户信息不存在", 201);
        }
        QueryWrapper<PointsTransaction> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userEntity.getId());
        queryWrapper.eq("activity_type", 6);

        if (baseMapper.selectCount(queryWrapper) <= 0 && status!= null && status.equals("2")){
            return true;
        }else if ((baseMapper.selectCount(queryWrapper) > 0 && userEntity.getIsFirst() >= 1) || userEntity.getIsFirst() >= 1) {
            return false;
        }else if (status != null && status.equals("1")){
            userEntity.setIsFirst(1);
            userService.updateById(userEntity);
            return false;
        }else if (baseMapper.selectCount(queryWrapper) > 0 && status!= null && status.equals("2")){
            return false;
        }
        return true;
    }
}